<template>
  <div class="ma-content-block lg:flex justify-between p-4">
    <!-- CRUD 组件 -->
    <ma-crud :options="options" :columns="columns" ref="crudRef">
    </ma-crud>
  </div>
</template>
<script setup>
import { ref, reactive } from 'vue'
import member from '@/api/member/member'
import { Message } from '@arco-design/web-vue'
import tool from '@/utils/tool'
import * as common from '@/utils/common'

const crudRef = ref()


const numberOperation = (newValue, id, numberName) => {
  member.numberOperation({ id, numberName, numberValue: newValue }).then( res => {
    res.success && Message.success(res.message)
  }).catch( e => { console.log(e) } )
}

const switchStatus = (statusValue, id, statusName) => {
  member.changeStatus({ id, statusName, statusValue }).then( res => {
    res.success && Message.success(res.message)
  }).catch( e => { console.log(e) } )
}

//
const options = reactive({
  id: 'tinkle_member',
  rowSelection: {
    showCheckedAll: true
  },
  pk: 'id',
  operationColumn: true,
  operationColumnWidth: 160,
  formOption: {
    viewType: 'modal',
    width: 800,
    layout: [
      {formType: 'grid', cols: [{span: 24, formList: [{dataIndex: 'avatar'}]}]},
      {formType: 'grid', cols: [{span: 12, formList: [{dataIndex: 'username'}]}, {span: 12, formList: [{dataIndex: 'password'}]}]},
      {formType: 'grid', cols: [{span: 12, formList: [{dataIndex: 'member_type'}]}, {span: 12, formList: [{dataIndex: 'member_status'}]}]},
    ]
  },
  api: member.getList,
  recycleApi: member.getRecycleList,
  add: {
    show: true,
    api: member.save,
    auth: ['member::save']
  },
  edit: {
    show: true,
    api: member.update,
    auth: ['member::update']
  },
  delete: {
    show: true,
    api: member.deletes,
    auth: ['member::delete'],
    realApi: member.realDeletes,
    realAuth: ['member::realDeletes']
  },
  recovery: {
    show: true,
    api: member.recoverys,
    auth: ['member::recovery']
  },
  import: {
    show: true,
    url: 'member/memberList/import',
    templateUrl: 'member/memberList/downloadTemplate',
    auth: ['member::import']
  },
  export: {
    show: true,
    url: 'member/memberList/export',
    auth: ['member::export']
  },
})

const columns = reactive([
  {
    title: "",
    dataIndex: "id",
    formType: "input",
    addDisplay: false,
    editDisplay: false,
    hide: true,
    commonRules: {
      required: true,
      message: "请输入"
    }
  },
  {
    title: "会员账号",
    dataIndex: "username",
    formType: "input",
    search: true,
    commonRules: [{ required: true, message: '会员账号不可为空' }, { match: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/, message: '会员账号只能为手机号，请输入正确' }]
  },
  {
    title: "会员密码",
    dataIndex: "password",
    formType: "input",
    addDefaultValue: '123456',
    hide: true,
    autocomplete: 'off',
    editDefaultValue: '',
    addDisabled: false, editDisabled: true, type: 'password',
    addRules: [{ required: true, message: '密码必填' }],
  },
  {
    title: "会员头像",
    dataIndex: "avatar",
    width: 75, formType: 'upload', returnType: 'hash',
    type: 'image', rounded: true, labelWidth: '86px'
  },
  {
    title: "会员类型",
    dataIndex: "member_type",
    formType: "radio",
    search: true,
    dict: {
      data: [
        {
          label: "普通会员",
          value: "1"
        },
        {
          label: "门店会员",
          value: "2"
        }
      ],
      translation: true
    },
    addDefaultValue: "2",
    // 控制门店表单部分显示和隐藏
    control: (val) => {
      if (val == 1) {
        // 普通用户，不显示门店数据
        return {
          store_name: {display: false},
          store_tel: {display: false},
          store_head_name: {display: false},
          store_address: {display: false},
        }
      } else {
        return {
          store_name: {display: true},
          store_tel: {display: true},
          store_head_name: {display: true},
          store_address: {display: true},
        }
      }
    }
  },
  {
    title: "用户状态",
    dataIndex: "member_status",
    formType: "radio",
    search: true,
    multiple: false,
    dict: {
      data: [
        {
          label: "正常",
          value: "1"
        },
        {
          label: "禁用",
          value: "2"
        }
      ],
      translation: true
    },
    addDefaultValue: "1"
  },
  {
    title: "门店名称",
    dataIndex: "store_name",
    formType: "input",
    search: true
  },
  {
    title: "门店电话",
    dataIndex: "store_tel",
    formType: "input",
    search: true
  },
  {
    title: "负责人姓名",
    dataIndex: "store_head_name",
    formType: "input",
    search: true
  },
  {
    title: "门店地址",
    dataIndex: "store_address",
    formType: "input",
    search: false
  },
  {
    title: "创建时间",
    dataIndex: "created_at",
    formType: "date",
    addDisplay: false,
    editDisplay: false,
    hide: true,
    showTime: true
  },
  {
    title: "更新时间",
    dataIndex: "updated_at",
    formType: "date",
    addDisplay: false,
    editDisplay: false,
    hide: true,
    showTime: true
  },
  {
    title: "删除时间",
    dataIndex: "deleted_at",
    formType: "date",
    addDisplay: false,
    editDisplay: false,
    hide: true,
    showTime: true
  }
])
</script>
<script> export default { name: 'member:' } </script>